package com.jin.lazytool.utils;

import com.jin.lazytool.JinTaskApplication;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class LogUtil {

    private static final Logger logger = Logger.getLogger(LogUtil.class.getName());
    private static final LogManager logManager = LogManager.getLogManager();

    static {
        InputStream inputStream = JinTaskApplication.class.getClassLoader().getResourceAsStream("logging.properties");
        try {
            logManager.readConfiguration(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        logManager.addLogger(logger);  //添加Logger
        String pattern = logManager.getProperty("java.util.logging.FileHandler.pattern");
        String logFilePath = pattern.substring(0, pattern.lastIndexOf("/"));
        File file = new File(logFilePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static void error(String msg,Throwable e){
        logger.log(Level.SEVERE,msg,e);
    }
    public static Logger logger(){
        return logger;
    }

}
